package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.PoemUtil;
import com.coding.dao.PoemDao;
import com.coding.dao.PoetDao;
import com.coding.pojo.PoemBasic;
import com.coding.pojo.Poet;
import com.coding.service.PoemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/26
 **/
@Service
public class PoemImpl implements PoemService {
    @Autowired
    PoemDao poemDao;
    @Autowired
    PoetDao poetDao;

    @Override
    public void getpoemlist(PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<PoemBasic> poemBasicList = poemDao.queryAll();
        for (PoemBasic poemBasic : poemBasicList
        ) {
            Poet poet = poetDao.selectByPrimaryKey(poemBasic.getPoet().getPoet_id());
            if (poet != null) {
                poemBasic.setPoet(poet);
            }
        }
        poemBasicList = PoemUtil.setPoemText(poemBasicList);
        PageInfo pageInfo = new PageInfo(poemBasicList);
        poemBasicList = pageInfo.getList();
        if (poemBasicList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的诗词");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的诗词共" + poemBasicList.size() + "个", poemBasicList);
    }

    @Override
    public void getpoembasic(int poem_id, BaseModel baseModel) throws Exception {
        PoemBasic poemBasic = poemDao.selectBasicById(poem_id);

        Poet poet = poetDao.selectByPrimaryKey(poemBasic.getPoet().getPoet_id());
        if (poet != null) {
            poemBasic.setPoet(poet);
        }

        if (poemBasic != null) {
            poemBasic = PoemUtil.setPoemText(poemBasic);
            baseModel.setSucceed("成功查询到诗词信息", poemBasic);

        } else {
            baseModel.setFailure("查无此诗");

        }

    }

    @Override
    public void getpoemByText(String poem_text, BaseModel baseModel) throws Exception {
        PoemBasic poemBasic = poemDao.selectByText(poem_text);
        if(poemBasic==null){
            baseModel.setFailure("查无此诗");
            return;
        }
        Poet poet = poetDao.selectByPrimaryKey(poemBasic.getPoet().getPoet_id());
        if (poet != null) {
            poemBasic.setPoet(poet);
        }
        if (poemBasic != null) {
            baseModel.setSucceed("成功查询到诗词信息", poemBasic);
        } else {
            baseModel.setFailure("查无此诗");
        }
    }

    @Override
    public void getpoemByMore(String more, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<PoemBasic> poemBasicList = poemDao.queryByMore(more);
        for (PoemBasic poemBasic : poemBasicList
        ) {
            Poet poet = poetDao.selectByPrimaryKey(poemBasic.getPoet().getPoet_id());
            if (poet != null) {
                poemBasic.setPoet(poet);
            }
        }
        poemBasicList = PoemUtil.setPoemText(poemBasicList);
        PageInfo pageInfo = new PageInfo(poemBasicList);
        poemBasicList = pageInfo.getList();
        if (poemBasicList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的诗词");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的诗词共" + poemBasicList.size() + "个", poemBasicList);
    }

    @Override
    public void getPoemByPlaceId(int place_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<PoemBasic> poemBasicList = poemDao.selectBasicByPlaceId(place_id);
        for (PoemBasic poemBasic : poemBasicList
        ) {
            Poet poet = poetDao.selectByPrimaryKey(poemBasic.getPoet().getPoet_id());
            if (poet != null) {
                poemBasic.setPoet(poet);
            }
        }
        poemBasicList = PoemUtil.setPoemText(poemBasicList);
        PageInfo pageInfo = new PageInfo(poemBasicList);
        poemBasicList = pageInfo.getList();
        if (poemBasicList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的诗词");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的诗词共" + poemBasicList.size() + "个", poemBasicList);
    }

    @Override
    public void searchPoem(String text, BaseModel baseModel) throws Exception {
        List<PoemBasic> poemBasicList=new ArrayList<>();
        String regEx = "[\\s+\r\n`~!@#$%^&*()-_—+=|{}':;',\\[\\].<《》>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
        String[] s =text.split(regEx);
        for (int i = 0; i <s.length ; i++) {
            List<PoemBasic> nameList= poemDao.searchByName(s[i]);
            if (nameList!=null){
                for (PoemBasic poem:nameList
                     ) {
                    if (checkPoem(poemBasicList,poem.getPoem().getPoem_id())){
                        poemBasicList.add(poem);
                    }
                }
            }

            List<PoemBasic> textList= poemDao.searchByText(s[i]);
            if (textList!=null){
                for (PoemBasic poem:textList
                ) {
                    if (checkPoem(poemBasicList,poem.getPoem().getPoem_id())){
                        poemBasicList.add(poem);
                    }
                }
            }

            List<PoemBasic> poetList= poemDao.searchByPoet(s[i]);
            if (poetList!=null){
                for (PoemBasic poem:poetList
                ) {
                    if (checkPoem(poemBasicList,poem.getPoem().getPoem_id())){
                        poemBasicList.add(poem);
                    }
                }
            }
        }
        if (poemBasicList.size()==0){
            baseModel.setFailure("未查询到相关信息");
        }else {
            baseModel.setSucceed("查询到相关诗词",poemBasicList);
        }
    }

    private boolean checkPoem(List<PoemBasic> poemBasicList,int poem_id){
        if (poemBasicList.size()==0){
            return true;
        }
        for (PoemBasic poem:poemBasicList
             ) {
            if (poem.getPoem().getPoem_id()==poem_id){
                return false;
            }
        }
        return true;
    }
}
